﻿package makeeasier.video {
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import fl.video.*;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	
	public class VideoLoop extends Sprite {
		
		private var movieInterval:Number;
		private var transitionTime:Number;
		private var timer:Timer;
		private var videoList:Array;
		private var videoPlayerList:Array;
		private var videoWidth:Number;
		private var videoHeight:Number;
		private var currentVideo:Number;
		private var dic:Dictionary;
		private var started:Boolean;
		
		public function VideoLoop(wd:Number = 320,hg:Number = 200){
			started = false;
			dic = new Dictionary();
			currentVideo = 0;
			videoWidth = wd;
			videoHeight = hg;
			movieInterval = 5000;
			transitionTime = 2;
			videoList = new Array();
			videoPlayerList = new Array();
			timer = new Timer(movieInterval);
			timer.addEventListener(TimerEvent.TIMER, nextVideo);
		}
		
		public function addVideo(s:String){
			videoList.push(s);
			createVideoPlayers();
			//trace(videoPlayerList);
		}
		
		public function nextVideo(e:Event):void{
			if(currentVideo == videoList.length){
				currentVideo = 0;
			}
			//trace(currentVideo);
			if(currentVideo > 0){
				var tween:Tween = new Tween(getChildAt(currentVideo-1), "alpha", Strong.easeIn, 1, 0, transitionTime, true);
				dic[tween] = tween;
			} else {
				var tween2:Tween = new Tween(getChildAt(videoList.length-1), "alpha", Strong.easeIn, 1, 0, transitionTime, true);
				dic[tween2] = tween2;
			}
			var tween3:Tween = new Tween(getChildAt(currentVideo), "alpha", Strong.easeIn, 0, 1, transitionTime, true);
			dic[tween3] = tween3;
			currentVideo++;
		}
		
		private function createVideoPlayers():void{
			for ( var i=0; i < videoList.length; i++ ){
				var videoPlayer:FLVPlayback = new FLVPlayback();
				videoPlayer.width = videoWidth;
				videoPlayer.height = videoHeight;
				//videoPlayer.x = 20+(i*50);
				//videoPlayer.y = 20+(i*50);
				videoPlayer.scaleMode = VideoScaleMode.EXACT_FIT;
				//videoPlayer.opaqueBackground = true;
				videoPlayer.alpha = 0;
				//videoPlayer.autoPlay = true;
				videoPlayer.autoRewind = true;
				videoPlayer.getVideoPlayer(videoPlayer.activeVideoPlayerIndex).smoothing = false;
				//videoPlayer.addEventListener("stoppedStateEntered", restartPlayer);
				//videoPlayer.addEventListener(MouseEvent.MOUSE_DOWN, function(e:Event){trace(e.target);});
				videoPlayer.stop();
				videoPlayerList.push(videoPlayer);
				videoPlayerList[i].source = videoList[i];
				videoPlayerList[i].play();
				addChild(videoPlayer);
			}
		}
		
		private function restartPlayer(e:Event):void{
			e.target.play();
		}
		
		public function startTimer():void{
			timer.start();
			createVideoPlayers();
			nextVideo(null);
			started = true;
		}
		
		public function stopTimer():void{
			timer.stop();
			if( numChildren > 0 ){
				while( numChildren == 0 ){
					removeChildAt(0);
				}
				videoPlayerList = new Array();
			}
			started = false;
		}
		
		public function changeVideo(to:Number):void{
			if(to==currentVideo){
				return;
			}
			var tween:Tween = new Tween(getChildAt(currentVideo), "alpha", Strong.easeIn, getChildAt(currentVideo).alpha, 0, transitionTime, true);
			tween.addEventListener(TweenEvent.MOTION_FINISH,function(e:Event){e.target.stop();});
			dic[tween] = tween;
			
			currentVideo = to;
			videoPlayerList[to].seek(0);
			var tween2:Tween = new Tween(getChildAt(to), "alpha", Strong.easeIn, getChildAt(to).alpha, 1, transitionTime, true);
			dic[tween2] = tween2;
			play();
		}
		
		public function play():void{
			videoPlayerList[currentVideo].play();
			videoPlayerList[currentVideo].addEventListener(VideoEvent.COMPLETE, function(e:Event){videoPlayerList[currentVideo].play();});
		}
		
		public function stop():void{
			videoPlayerList[currentVideo].stop();
		}

	}
	
}